<template>
  <div class="app-container">
    <!--选择仓库-->
    <WarehouseInDetailEdit ref="openWarehouseInDetailEdit" @refresh="refresh"></WarehouseInDetailEdit>
    <!-- 货物入仓详情 -->
    <div class="detail-title-box">
      <span>详情信息</span>
    </div>
    <el-form ref="form" :model="detailsQuery" size="small" :inline="true" :label-position="labelPosition" label-width="120">
      <el-form-item label="编号:" prop="pzbh">
        <el-input
          v-model="detailsQuery.pzbh"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="外部单号:" prop="wbdh">
        <el-input
          v-model="detailsQuery.wbdh"
          placeholder="请输入外部单号"
          clearable
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="车牌" prop="dzzy6">
        <el-input
          v-model="detailsQuery.dzzy6"
          placeholder="请输入车牌"
          clearable
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="司机" prop="dzzy7">
        <el-input
          v-model="detailsQuery.dzzy7"
          placeholder="请输入司机姓名"
          clearable
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="dzzy8">
        <el-input
          v-model="detailsQuery.dzzy8"
          placeholder="请输入电话"
          clearable
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="dzzy10">
        <el-input
          v-model="detailsQuery.dzzy10"
          placeholder="请输入身份证"
          clearable
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="备注" prop="dzbz">
        <el-input
          v-model="detailsQuery.dzbz"
          placeholder="请输入备注"
          clearable
          :disabled="true"
        />
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="warehouseInDetails"
      show-summary
      :summary-method="getSummaries"
    >
      <af-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
          >查看明细</el-button>
        </template>
      </af-table-column>
      <af-table-column prop="ssrqid" label="入仓日期"  fixed="left"></af-table-column>
      <af-table-column prop="spzs" label="物品规格"  ></af-table-column>
      <af-table-column prop="spbh" label="批号" ></af-table-column>
      <af-table-column prop="spgh" label="柜号" ></af-table-column>
      <af-table-column prop="spjs" label="件数" ></af-table-column>
      <af-table-column prop="spjsl" label="单重(Kg)" ></af-table-column>
      <af-table-column prop="spsl" label="重量(吨)" ></af-table-column>
      <af-table-column prop="spcd" label="品牌" ></af-table-column>
      <af-table-column prop="spckmc" label="仓库" ></af-table-column>
      <af-table-column prop="ckcw" label="仓位" width="80"></af-table-column>
      <af-table-column prop="spkhmc" label="客户" ></af-table-column>
      <af-table-column prop="sptm" label="客户条码" ></af-table-column>
      <af-table-column prop="sptmc" label="仓库条码" ></af-table-column>
      <af-table-column prop="spbz" label="备注"></af-table-column>
    </el-table>
    <el-row type="flex" justify="center" :gutter="5" class="mt20">
      <el-col :span="1.5">
        <el-button @click="goBack">返回</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import WarehouseInDetailEdit from './warehouseInDetailEdit.vue'
import { getWarehouseInDetails } from "@/api/storage/commonGoods/warehouseIn";
import { NUMBER_DIDGIT, WEIGHT_DIGIT } from '@/utils/constant'

export default {
  name: "CWarehouseInDetail",
  components: { WarehouseInDetailEdit },
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //对齐方式
      labelPosition:"right",
      //表格加载
      loading: true,
      // 详情信息
      detailsQuery: {
       	dzbz: "",
        dzid: "",
        dzzy10: "",
        dzzy6: "",
        dzzy7: "",
        dzzy8: "",
        iszxf: "",
        iszyl: "",
        wbdh: "",
        pzbh:"",
        khmc:""
      },
      //获取详情参数
      queryParams:{
        dzid:"",
        shztn:"0"
      },
      //行数据
      warehouseInDetails:[],
      //页数
      pageSize: 999,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const query = to.query
    next(vm => {   
      if(Object.keys(query).length > 0) {
        vm.reset(vm)
        vm.queryParams.dzid =  query.dzid
        vm.getList()
      }
    })
  },
  methods: {
    /**获取详情数据 */
    getList() {
      this.loading = true
      getWarehouseInDetails(this.queryParams).then(response => {
        const data = response.data
        if(data && data.info) {
          this.detailsQuery = data.info
        }
        if(data && data.list && data.list.length) this.warehouseInDetails = data.list
        this.loading = false
      });
    },

     //合计
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = "合计"
			    return
        }
        if ((column.property === "spjs" || column.property === "spsl")) {
          const values = data.map((item) => {
            return parseFloat(this.delcommafy(item[column.property]))
          });
          sums[index] = values.reduce((prev, curr) => {
            if(!prev) prev = 0
            if(!curr) curr = 0
            if(column.property === "spsl") {
              return parseFloat((prev + curr).toFixed(WEIGHT_DIGIT))
            }
            if(column.property === "spjs") {
              return parseFloat((prev + curr).toFixed(NUMBER_DIDGIT))
            }
          }, 0);
        }
      })
      return sums
    },

     /**编辑行 */
     handleEdit(row) {
      if(!row.itbz) row.itbz = ""
      const data = JSON.parse(JSON.stringify(row))
      this.$refs.openWarehouseInDetailEdit.open(data, this.queryParams.dzid)
    },

     /** 刷新数据*/
    refresh() {
      this.getList()
    },
     //跳回货物入仓页
    goWarehouseIn() {
      this.$tab.closePage(this.$route)
      this.$router.push({name:"CWarehouseIn", params:{refresh:true}})
    },
    reset(vm) {
      Object.assign(vm.$data, vm.$options.data())
    },
     /**返回不刷新 */
    goBack() {
      this.$router.push({name:"CWarehouseIn"})
    }
  }
};
</script>
